#include "../include/PageLib.hpp"

PageLib::PageLib(Configuration &conf, DirScanner &dirScanner,
				 FileProcessor &fileProcessor, Deduplication &del)
	: _conf(conf), _dirScanner(dirScanner), _fileProcessor(fileProcessor), _del(del)
{
}

PageLib::~PageLib()
{
}

void PageLib::create()
{
	// 生成_configMap（保存配置数据的键值对）
	//_conf.getConfigMap();
	// 生成_files的vector（存储所有语料文件的绝对路径）
	_dirScanner(_conf.searchConfigMap("xmlResource"));
	// 生成ripepage.dat和offset.dat
	_fileProcessor.process(_dirScanner.getFiles());
}

void PageLib::store()
{
	string ripepage = _conf.searchConfigMap("ripepage");
	string offset = _conf.searchConfigMap("offset");
	// 打开ofs_ripepage、ofs_offset两个文件输出流
	ofstream ofs_ripepage(ripepage, std::ios::trunc);
	if (!ofs_ripepage)
	{
		cout << "openfile failed" << endl;
#ifdef DEBUG
		perror("PageLib::store()_ofs_ripepage() failed");
		cout << "filepath: " << ripepage << endl;
#endif
		return;
	}

	ofstream ofs_offset(offset, std::ios::trunc);
	if (!ofs_offset)
	{
		cout << "openfile failed" << endl;
#ifdef DEBUG
		perror("PageLib::store()_ofs_offset() failed");
		cout << "filepath: " << offset << endl;
#endif
		return;
	}

	string docBegin("<doc>\n");
	string docEnd("</doc>\n");
	string docIdBegin("<docid>");
	string docIdEnd("</docid>\n");
	string docUrlBegin("<url>");
	string docUrlEnd("</url>\n");
	string docTitleBegin("<title>");
	string docTitleEnd("</title>\n");
	string docContentBegin("<content>");
	string docContenEnd("</content>\n");

	long cnt = 1;
	for (auto &proFile : _fileProcessor.getVecProFiles())
	{
		string rssData("");
		rssData.append(docBegin)
			.append(docIdBegin)
			.append(std::to_string(cnt))
			.append(docIdEnd)
			.append(docUrlBegin)
			.append(proFile.link)
			.append(docUrlEnd)
			.append(docTitleBegin)
			.append(proFile.title)
			.append(docTitleEnd)
			.append(docContentBegin)
			.append(proFile.description)
			.append(docContenEnd)
			.append(docEnd);

		// 向 ofs_ripepage流 输入 rssData 数据，得到 ripepage.dat
		int beg = ofs_ripepage.tellp();
		ofs_ripepage << rssData;
		int end = ofs_ripepage.tellp();
		// 向 map _offset 中插入键值对(beg, end - beg)
		_offset[cnt] = std::make_pair(beg, end - beg);
		++cnt;
	}

	// 向 ofs_offset流 输入 (beg, end - beg)键值对 数据，得到 offset.dat
	for (auto &offset : _offset)
	{
		ofs_offset << offset.first << " " << offset.second.first << " " << offset.second.second << endl;
	}

	ofs_ripepage.close();
	ofs_offset.close();
}

void PageLib::deduplicate()
{
	_del(_fileProcessor.getVecProFiles(), 5);
}